package tree;

public class L543 {
    public static int diameterOfBinaryTree(TreeNode root) {

        if (root==null){
            return 0;
        }
        int le= depth(root.left);
        int re= depth(root.right);
        int curr=le+re;
        int ledia = diameterOfBinaryTree(root.left);
        int redia=diameterOfBinaryTree(root.right);
        return Math.max(curr, Math.max(ledia,redia));

    }

    private static int depth(TreeNode left) {
        if (left ==null){
            return 0;
        }
        return 1 + Math.max(depth(left.left), depth(left.right));

    }

    public static void main(String[] args) {
        // 构造二叉树的节点
        TreeNode node4 = new TreeNode(5, null, null);
        TreeNode node3 = new TreeNode(4, null, null);
        TreeNode node2 = new TreeNode(3, null, null);
        TreeNode node1 = new TreeNode(2, node3, node4);
        TreeNode root = new TreeNode(1, node1, node2);

        // 假设printByLevels方法是你已经实现的
        int i = diameterOfBinaryTree(root);
        // 打印结果
        System.out.println(i);
    }

}
